package com.yz.mcd.model;

import android.app.Activity;
import android.util.Log;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.yz.mcd.BaseApplication;
import com.yz.mcd.base.bcl.BaseBean;
import com.yz.mcd.base.function.HttpUrlProvider;
import com.yz.mcd.base.imp.BaseActivityImp;
import com.yz.mcd.base.imp.BaseModelImp;
import com.yz.mcd.bean.UserInfoBean;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by XYLi on 2017/4/21.
 */

public class UserInfoModel implements BaseModelImp{

    private String TAG="model.UserInfoModel";
    private BaseActivityImp act;
    private Object data;

    public UserInfoModel(BaseActivityImp activity) {
        this.act = activity;
    }


    public void handleInfo(String type, BaseBean bean) {
        JSONObject jsObj;
        switch (type)
        {
            case "Ac_reg_1":
                jsObj=registerAccount((UserInfoBean)bean);
                break;
            case "Ac_log_1":
                jsObj=loginAccount((UserInfoBean)bean);
                break;
        }
    }


    public JSONObject registerAccount(final UserInfoBean user)
    {
        JSONObject jsObj=new JSONObject();
        JsonObjectRequest jsObjRequest = new JsonObjectRequest
                (
                        Request.Method.GET,
                        HttpUrlProvider.getRegister(user),
                        jsObj,
                        new Response.Listener<JSONObject>() {
                            @Override
                            public void onResponse(JSONObject jsObj) {

                            }
                        },
                        new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError volleyError) {
                                Log.i("asdasd", "asdasd");
                            }
                        }
                );
        BaseApplication.getRequestQueue().add(jsObjRequest);
        Log.e(TAG, jsObj.toString());
        return jsObj;
    }

    public JSONObject loginAccount(final UserInfoBean user)
    {
        JSONObject jsObj=new JSONObject();
        JsonObjectRequest jsObjRequest = new JsonObjectRequest
                (
                        Request.Method.GET,
                        HttpUrlProvider.getLogin(user),
                        jsObj,
                        new Response.Listener<JSONObject>() {
                            @Override
                            public void onResponse(JSONObject jsObj) {
                                Log.e(TAG, jsObj.toString());
                                Log.e(TAG,"aaaaaaaaaaaaa");
                                try {
                                    String tmpstr=jsObj.getString("success");
                                    Log.e(TAG,tmpstr);
                                    switch (tmpstr)
                                    {
                                        case "1":
                                            UserInfoBean root_user=BaseApplication.getUserInfo();
                                            root_user.setPhone(user.getPhone());
                                            root_user.setUser_pwd(user.getUser_pwd());
                                            BaseApplication.setRequestKey(jsObj.getString("data"));
                                            act.infoChangeEvent("Ac_log_1",null);
                                            break;
                                        case "0":
                                            BaseApplication.cout(jsObj.getString("info"));
                                            act.infoChangeEvent("Ac_log_10",null);
                                            break;
                                    }
                                }catch (JSONException e)
                                {
                                    e.getMessage();
                                }
                            }
                        },
                        new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError volleyError) {
                                Log.i("asdasd", "asdasd");
                            }
                        }
                );
        BaseApplication.getRequestQueue().add(jsObjRequest);
        return jsObj;
    }


}
